//-----------------------------------------------------------------------------
// File: util_image.h
//
// Desc: image utility.
//       build20070218
//
// Author: Silva Lam
//-----------------------------------------------------------------------------
#ifndef _UTIL_IMAGE_H_
#define _UTIL_IMAGE_H_
#include "xtypes.h"
#include "util_datadef.h"
#include "xblob.h"
#include "xarray.h"
#include "util_canvas.h"


#define DIB_ALIGN (sizeof(DWORD)-1)			//align by 4 byte

#define DIB_WIDTH_BYTES(nWidth, nBytes)	\
	(nWidth*nBytes)

#define DIB_PITCH_BYTES(nWidth, nBytes)	\
	((DIB_WIDTH_BYTES(nWidth, nBytes) + DIB_ALIGN) & (~DIB_ALIGN))

#define DIB_PAD_BYTES(nWidth, nBytes)	\
	(DIB_PITCH_BYTES(nWidth, nBytes) - DIB_WIDTH_BYTES(nWidth, nBytes))


#define CANVAS_ALIGN (2*sizeof(DWORD)-1)	//align by 8 byte 

#define CANVAS_WIDTH_BYTES(nWidth, nBytes)	\
	(nWidth*nBytes)

#define CANVAS_PITCH_BYTES(nWidth, nBytes)	\
	((CANVAS_WIDTH_BYTES(nWidth, nBytes) + CANVAS_ALIGN) & (~CANVAS_ALIGN))

#define CANVAS_PAD_BYTES(nWidth, nBytes)	\
	(CANVAS_PITCH_BYTES(nWidth, nBytes) - CANVAS_WIDTH_BYTES(nWidth, nBytes))


typedef struct {
	DWORD      biSize;
	LONG       biWidth;
	LONG       biHeight;
	WORD       biPlanes;
	WORD       biBitCount;
	DWORD      biCompression;
	DWORD      biSizeImage;
	LONG       biXPelsPerMeter;
	LONG       biYPelsPerMeter;
	DWORD      biClrUsed;
	DWORD      biClrImportant;
}bmpih_t;

#pragma pack(2)
typedef struct {
	WORD    bfType;
	DWORD   bfSize;
	WORD    bfReserved1;
	WORD    bfReserved2;
	DWORD   bfOffBits;
}bmpfh_t;
#pragma pack()

#pragma pack(1)
typedef struct{
	BYTE	manufacturer;		//must be 0x0A
	BYTE	version;			//5-256color
	BYTE	encoding;			//1-rle
	BYTE	bits_per_pixel;		//8-256color
	WORD	xmin,ymin;			//0
	WORD	xmax,ymax;			//important
	WORD	hres;				//0
	WORD	vres;				//0
	BYTE	palette[48];		//0
	BYTE	reserved;
	BYTE	colour_planes;		//important
	WORD	bytes_per_line;		//important
	WORD	palette_type;		//0
	BYTE	filler[58];			//0
}pcxh_t;
#pragma pack()





bmpfh_t fnBmpFileHeader(DWORD bfSize);
bmpih_t fnBmpInfoHeader(LONG biWidth, LONG biHeight, WORD biPlanes,	WORD biBitCount);
void LoadCanvasFromBitmap32(CxSphereCanvas& canvas, lpcstr_t lpszPathName);
void SaveCanvasAsBitmap32(lpcstr_t lpszPathName, CxSphereCanvas& canvas);
void LoadCanvasFromBitmap24(CxSphereCanvas& canvas, lpcstr_t lpszPathName);
void LoadCanvasFromPCX(CxSphereCanvas& canvas, lpcstr_t lpszPathName);






void dda_line(CxSphereCanvas& canvas, int xa,int ya,int xb,int yb, DWORD cr=0x00000000);
void dda_line_ip(CxSphereCanvas& canvas, int xa,int ya,int xb,int yb, DWORD crs=0x00000000, DWORD cre=0x00000000);
void bresenham_line(CxSphereCanvas* pCanvas, int_t x0,int_t y0,int_t x1,int_t y1, DWORD cr=0x00000000);
void raster_circle(CxSphereCanvas* pCanvas, int_t x0, int_t y0, int_t radius, DWORD cr=0x00000000);





//--------------------------------------------------------------------------

#endif //_UTIL_IMAGE_H_
